package cn.qidisoft.edu.hzjt.service.zyc.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.zyc.DeveAmateurSchoolMapper;
import cn.qidisoft.edu.hzjt.model.xb.PartyResponsibilityAttEn;
import cn.qidisoft.edu.hzjt.model.zyc.DeveAmateurSchool;
import cn.qidisoft.edu.hzjt.model.zyc.DeveAmateurSchoolAtt;
import cn.qidisoft.edu.hzjt.model.zyc.DeveAmateurSchoolStudent;
import cn.qidisoft.edu.hzjt.service.zyc.IDeveAmateurSchoolService;
import cn.qidisoft.edu.hzjt.utils.LogType;

@Service
@Transactional
public class DeveAmateurSchoolServiceImpl implements IDeveAmateurSchoolService {
	protected static final LoggerAdapter LOGGER = LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);

	@Autowired
	private DeveAmateurSchoolMapper deveAmateurSchoolMapper;

	@Override
	public PaginationBean<DeveAmateurSchool> queryDeveAmateurSchoolPageList(Map<String, Object> qMap,
			Page page) {
		try {

			int c = deveAmateurSchoolMapper.queryDeveAmateurSchoolPageCount(qMap);
			int totalRecords = 0;
			totalRecords = (int) c;

			PaginationBean<DeveAmateurSchool> pageHolder = new PaginationBean<DeveAmateurSchool>(page, totalRecords);

			List<DeveAmateurSchool> resultList = deveAmateurSchoolMapper.queryDeveAmateurSchoolPageList(
					qMap, new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
							pageHolder.getPageSize()));
			pageHolder.setPageList(resultList);
			return pageHolder;
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("queryStudentDemoPageList [{}] error," + this.getClass().getName() + "=[{}]",
					this.getClass().getName(),
					ToStringBuilder.reflectionToString(qMap, ToStringStyle.SHORT_PREFIX_STYLE));
			throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
		}

	}

	@Override
	public void insert(DeveAmateurSchool data) {
		deveAmateurSchoolMapper.insert(data);
		List<DeveAmateurSchoolStudent> deveAmateurSchoolStudentsList = data.getDeveAmateurSchoolStudentsList();
		if (deveAmateurSchoolStudentsList != null && deveAmateurSchoolStudentsList.size() > 0) {
			for (int i = 0; i < deveAmateurSchoolStudentsList.size(); i++) {
				DeveAmateurSchoolStudent deveAmateurSchoolStudent = deveAmateurSchoolStudentsList.get(i);
				deveAmateurSchoolStudent.setParentId(data.getDxid());
				deveAmateurSchoolMapper.insertDeveAmateurSchoolStudent(deveAmateurSchoolStudent);
			}
		}
	}

	@Override
	public DeveAmateurSchool queryDeveAmateurSchoolById(Integer dxid) {
		return deveAmateurSchoolMapper.queryDeveAmateurSchoolById(dxid);
	}

	@Override
	public void update(DeveAmateurSchool deveAmateurSchool) {

		deveAmateurSchoolMapper.update(deveAmateurSchool);
		// 先刪除
		deveAmateurSchoolMapper.deletedeveAmateurSchoolStduentByParentId(deveAmateurSchool.getDxid());
		// 再添加
		List<DeveAmateurSchoolStudent> deveAmateurSchoolStudentsList = deveAmateurSchool
				.getDeveAmateurSchoolStudentsList();
		if (deveAmateurSchoolStudentsList != null && deveAmateurSchoolStudentsList.size() > 0) {
			for (int i = 0; i < deveAmateurSchoolStudentsList.size(); i++) {
				DeveAmateurSchoolStudent deveAmateurSchoolStudent = deveAmateurSchoolStudentsList.get(i);
				deveAmateurSchoolStudent.setParentId(deveAmateurSchool.getDxid());
				deveAmateurSchoolMapper.insertDeveAmateurSchoolStudent(deveAmateurSchoolStudent);
			}
		}
	}

	@Override
	public List<DeveAmateurSchool> getDASList(DeveAmateurSchool pM2) {
		return deveAmateurSchoolMapper.getDASList(pM2);
	}

	@Override
	public void deleteByIds(List<Integer> id_List) {
		for (int i = 0; i < id_List.size(); i++) {
			deveAmateurSchoolMapper.deletedeveAmateurSchoolStduentByParentId(id_List.get(i));
		}
		deveAmateurSchoolMapper.deleteByIds(id_List);
	}

	@Override
	public void batchInsertsFiles(List<PartyResponsibilityAttEn> records) {
		deveAmateurSchoolMapper.batchInsertsFiles(records);
	}

	@Override
	public List<PartyResponsibilityAttEn> queryFilesList(PartyResponsibilityAttEn entity) {
		return deveAmateurSchoolMapper.queryFilesList(entity);
	}

	// 附件删除
	@Override
	public int deleteAccessory(PartyResponsibilityAttEn entity) {
		deveAmateurSchoolMapper.deleteAccessory(entity);
		return 0;
	}

	// 查文件地址
	@Override
	public DeveAmateurSchoolAtt selPartyAmateurSchoolAttById(DeveAmateurSchoolAtt deveAmateurSchoolAtt) {
		return deveAmateurSchoolMapper.selPartyAmateurSchoolAttById(deveAmateurSchoolAtt);
	}

	public DeveAmateurSchoolStudent getParendById(Integer xsid) {
		return deveAmateurSchoolMapper.getParendById(xsid);
	}
}
